/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.utils.executor;


import com.intelligent.ispc.common.utils.ApplicationException;

import java.util.Map;

/**
 * Created by Peter on 2016/9/7.
 */
public class ExecutorTask implements Runnable {
    private String name;
    private Work work;
    private ExceptionWork exceptionWork;
    private boolean stop = false;
    private String parentId;

    /**
     * Executor task constructor.
     *
     * @param name work name.
     * @param work work instance.
     */
    public ExecutorTask(String name, Work work) {
        this.name = name;
        this.work = work;
    }

    /**
     * Executor task constructor.
     *
     * @param name          work name.
     * @param work          work instance.
     * @param exceptionWork work call back instance.
     */
    public ExecutorTask(String name, Work work, ExceptionWork exceptionWork) {
        this.name = name;
        this.work = work;
        this.exceptionWork = exceptionWork;
    }

    /**
     * Executor task constructor.
     *
     * @param name     work name.
     * @param work     work instance.
     * @param parentId parent id..
     */
    public ExecutorTask(String name, String parentId, Work work) {
        this.name = name;
        this.work = work;
        this.parentId = parentId;
    }

    @Override
    public void run() {
        while (!Thread.interrupted() && !stop) {
            try {
                Thread.sleep(100);
                work.execute();
                Thread.currentThread().interrupt();
            } catch (ApplicationException e) {
                e.printStackTrace();
                if (exceptionWork != null) {
                    exceptionWork.execute(e.getMessage());
                }
                Thread.currentThread().interrupt();
            } catch (Exception e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        }
    }

    /**
     * Shut down work.
     */
    public void shutdown() {
        this.stop = true;
        work.stop();
        Map<String, ExecutorTask> pool = ExecutorPool.getTasks();
        String currentName = Thread.currentThread().getName();
        for (ExecutorTask task : pool.values()) {
            if (task.getParentId() == currentName) {
                task.shutdown();
            }
        }
        Thread.currentThread().interrupt();
    }

    /**
     * Work interface.
     */
    public interface Work {

        /**
         * Execute work.
         */
        void execute();

        /**
         * Stop work.
         */
        void stop();
    }

    /**
     * Exception Work.
     */
    public interface ExceptionWork {
        /**
         * Execute with message.
         *
         * @param msg message.
         */
        void execute(String msg);
    }

    public String getName() {
        return name;
    }

    public String getParentId() {
        return parentId;
    }

    public void setName(String name) {
        this.name = name;
    }
}
